﻿using SlaughterItem.Common.Domains.Detection;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;

namespace WinFormsApp1.TongShiWei.COA
{
    public partial class ShowCOAData :UIForm
    {
        public ShowCOAData()
        {
            InitializeComponent();
            ShowCOAData_Now();
        }
        int AddNum = 0;
        public async void ShowCOAData_Now()
        {
            this.dataGridView1.Rows.Clear();
            //http://localhost:5159/api/TSW/ShowCOAData_
            var res = await HttpClientHelper.RequestUrl<COAData, ApiResult<List<COAData>>>(URLCommon.Url2, "/TSW/ShowCOAData_", HttpClientHelper.MethodType.GET);
            if (res.Code > 0)
            {
                foreach (var item in res.Data)
                {
                    this.dataGridView1.Rows.Add(item.COAId, item.LiveAnimalsDocumentTime.ToString("yyyy-MM-dd"), item.LiveAnimalsDocument, item.COAQuarantineType, item.COAGoods, item.CheckInBatch, item.COAQuarantineType, item.COATime.ToString("yyyy-MMFile-dd"), item.COAAuditState == 0 ? "未审核" : (item.COAAuditState == 1 ? "审核通过" : "审核失败"), item.COACreateName, item.COACreateTime.ToString("yyyy-MM-dd"));
                }
                if (AddNum == 0)
                { 
                    DataGridViewButtonColumn dataGridViewButtonColumn = new DataGridViewButtonColumn();
                    dataGridViewButtonColumn.UseColumnTextForButtonValue = true;
                    dataGridViewButtonColumn.HeaderText = "修改";
                    dataGridViewButtonColumn.Text = "修改";
                    DataGridViewButtonColumn dataGridViewButtonColumn2 = new DataGridViewButtonColumn();
                    dataGridViewButtonColumn2.UseColumnTextForButtonValue = true;
                    dataGridViewButtonColumn2.HeaderText = "删除";
                    dataGridViewButtonColumn2.Text = "删除";
                    this.dataGridView1.CellClick+=new DataGridViewCellEventHandler(DelCOAData);
                    this.dataGridView1.Columns.AddRange(dataGridViewButtonColumn, dataGridViewButtonColumn2);
                    AddNum++;
                }
            }
        }
        //删除检验报告表数据
        public async void DelCOAData(object sender,DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 12)
            {
                //http://localhost:5159/api/TSW/DelCOAData_
                int COAId_ =Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells[0].Value);
                DelCOAId delCOAId = new DelCOAId(COAId_);
                var res=await HttpClientHelper.RequestUrl<DelCOAId, ApiResult<DelCOAId>> (URLCommon.Url1, "/TSW/DelCOAData_", HttpClientHelper.MethodType.POST, delCOAId);
                if (res.Code > 0)
                {
                    MessageBox.Show("删除成功");
                    ShowCOAData_Now();
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
            else
            {
                return;
            }
        }
        public record DelCOAId(int COAId);
        public class COAData
        {
            /// <summary>
            /// 检疫报告单ID
            /// </summary>
            public int COAId { get; set; }
            /// <summary>
            /// 单据ID
            /// </summary>
            public string LiveAnimalsDocument { get; set; } = string.Empty;
            /// <summary>
            /// 单据日期
            /// </summary>
            public DateTime LiveAnimalsDocumentTime { get; set; } = DateTime.Now;
            /// <summary>
            /// 检验报告类型
            /// </summary>
            public string COARType { get; set; } = string.Empty;
            /// <summary>
            /// 检测商品
            /// </summary>
            public string COAGoods { get; set; } = string.Empty;
            /// <summary>
            /// 商品批号
            /// </summary>
            public string CheckInBatch { get; set; } = string.Empty;
            /// <summary>
            /// 检验类型
            /// </summary>
            public string COAQuarantineType { get; set; } = string.Empty;
            /// <summary>
            /// 样品来源
            /// </summary>
            public string OriginAddr { get; set; } = string.Empty;
            /// <summary>
            /// 样品规格
            /// </summary>
            public string SampleSize { get; set; } = string.Empty;
            /// <summary>
            /// 样品数量
            /// </summary>
            public int SampleNum { get; set; }
            /// <summary>
            /// 到样日期
            /// </summary>
            public DateTime SampleTime { get; set; } = DateTime.Now;
            /// <summary>
            /// 样品形态
            /// </summary>
            public string SampleData { get; set; } = string.Empty;
            /// <summary>
            /// 检验日期
            /// </summary>
            public DateTime COATime { get; set; } = DateTime.Now;
            /// <summary>
            /// 执行标准
            /// </summary>
            public string ExecuteData { get; set; } = string.Empty;
            /// <summary>
            /// 审核状态
            /// </summary>
            public int COAAuditState { get; set; }
            /// <summary>
            /// 制单人
            /// </summary>
            public string COACreateName { get; set; } = string.Empty;
            /// <summary>
            /// 制单日期
            /// </summary>
            public DateTime COACreateTime { get; set; } = DateTime.Now;
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool ISDeleteCOA { get; set; }
            /// <summary>
            /// 检疫证
            /// </summary>
            public string COAImage { get; set; } = string.Empty;
            /// <summary>
            /// 生产单位
            /// </summary>
            public string ProductionName { get; set; } = string.Empty;
            /// <summary>
            /// 检验结论
            /// </summary>
            public string COAverdict { get; set; } = string.Empty;
        }
    }
}
